package com.ymatou.iapi.coupon.testcase;

import java.util.ArrayList;
import java.util.List;









import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.ConversionCouponBean;
import com.ymatou.iapi.coupon.parameter.getUserUsableCouponQuantityBean;
import com.ymatou.iapi.coupon.service.getUserUsableCouponQuantityCall;
import com.ymatou.iapi.couponmanage.parameter.CreateBean;
import com.ymatou.iapi.couponmanage.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.parameter.executeTaskBean;
import com.ymatou.iapi.couponmanage.service.CreateCall;
import com.ymatou.iapi.couponmanage.service.CreateCouponCall;
import com.ymatou.iapi.couponmanage.service.executeTaskCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceCallServiceV2;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.couponconversion;
import com.ymttest.database.model.couponconversioncode;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponconversionWapper;
import com.ymttest.database.sqlwapper.couponconversioncodeWapper;
import com.ymttest.database.sqlwapper.couponsendsubtaskWapper;
//import com.ymttest.database.model.couponbatch;
//import com.ymttest.database.model.couponconversion;
//import com.ymttest.database.model.couponconversioncode;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.sqlwapper.couponconversionWapper;
//import com.ymttest.database.sqlwapper.couponconversioncodeWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*获取用户可用的优惠券数量
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getUserUsableCouponQuantity{
	private static getUserUsableCouponQuantityBean getuserusablecouponquantityBean;
	private static getUserUsableCouponQuantityCall getuserusablecouponquantityCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户可用的优惠券数量");
	}

	@Before
	public void caseUp() {
		getuserusablecouponquantityBean=new getUserUsableCouponQuantityBean();
		getuserusablecouponquantityCall=new getUserUsableCouponQuantityCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getuserusablecouponquantityCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_001() {
		Logger.start(true,"获取用户可用的优惠券数量");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();

			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode1, userId);
			
			// 平台券
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			String batchCode2 = couponCallService.createPlatformCoupon(createCouponBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode2, userId);

			getuserusablecouponquantityBean.setUserId(userId);
			
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("2", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_002() {
		Logger.start(true,"UserId空");
		try {
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_003() {
		Logger.start(true,"UserId不存在");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			getuserusablecouponquantityBean.setUserId(userId);

			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_004() {
		Logger.start(true,"获取用户可用的优惠券数量-商家券");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	

			for (int i = 0; i < 5; i++) {
	
			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();

			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode1, userId);
			}
			getuserusablecouponquantityBean.setUserId(userId);
			
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("5", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_005() {
		Logger.start(true,"获取用户可用的优惠券数量-平台领取券");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	
			for (int i = 0; i < 10; i++) {
					
			// 平台券
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			String batchCode2 = couponCallService.createPlatformCoupon(createCouponBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode2, userId);
			}
			
			getuserusablecouponquantityBean.setUserId(userId);
			
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("10", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_006() {
		Logger.start(true,"获取用户可用的优惠券数量-兑换券");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();

			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode1, userId);
			
			// 平台券
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			String batchCode2 = couponCallService.createPlatformCoupon(createCouponBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode2, userId);
			

			// 平台兑换优惠券
			CouponServiceCallServiceV2 CouponServiceCallServiceV2 = new CouponServiceCallServiceV2();
//
//				// createcouponBean
//			CreateCouponBean createcouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
//
//				String batchcode2 = couponCallService.createPlatformCoupon(createcouponBean).getBatchCode();
//				List<couponbatch> couponbatchs = new couponbatchWapper().selectBybatchCode(batchcode2);
//
//					List<String> batchCodes = new ArrayList<>();
//					batchCodes.add(batchcode2);
//
//					CreateBean createBean = couponCallService.setDefaultRandomCreateBean(batchCodes);
//					CreateCall createCall = couponCallService.create(createBean);
//				
//					// 等待兑换码生成
//					couponconversion bill = new couponconversionWapper()
//							.selectByconversionName(createBean.getBillName()).get(0);
//					List<couponconversioncode> couponconversioncodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
//					
//					int flag=0;
//					while(couponconversioncodes.size()<createBean.getGenerateQuantity() && flag<500)
//					{
//						flag++;
//						Thread.sleep(200);
//						couponconversioncodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());	
//					}
			//生成批次及兑换码
			CouponServiceMCallServiceV2 couponMCallService = new CouponServiceMCallServiceV2();
			CreateCouponBean createcouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createcouponBean);

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = couponMCallService.setDefaultRandomCreateBean(batchCodes);
			CreateCall createCall = couponMCallService.create(createBean);
		
			// 这里直接调用job接口生成兑换码
			List<Map>  tasks=new couponsendsubtaskWapper().selectTaskByTaskTypelimit(3,1);
			executeTaskCall call=new executeTaskCall();
			executeTaskBean bean=new executeTaskBean();
			
			for(int i=0;i<tasks.size();i++){
				bean.setSubTaskId(Integer.parseInt(tasks.get(i).get("SubTaskId").toString()));
				call.setData(bean);
				call.callService();
			}
			couponconversion bill = new couponconversionWapper().selectByconversionName(createBean.getBillName()).get(0);
	        List<couponconversioncode> couponconversioncodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
	        Logger.verifyEquals(createBean.getGenerateQuantity(), couponconversioncodes.size(), "验证数据CouponConversionCode中生成相应记录");
			
			ConversionCouponBean conversionCouponBean = CouponServiceCallServiceV2.setConversionCouponBean(userId,couponconversioncodes.get(0).getConversionCode());
			
			String batchCode3 = CouponServiceCallServiceV2.ConversionCoupon(conversionCouponBean).getCouponCodes().get(0);
				
			couponCallService.receiveCouponInfo(batchCode3, userId);

			getuserusablecouponquantityBean.setUserId(userId);
			
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("3", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}